package com.itheima.tlias84.Service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.Po.LoginParam;
import com.itheima.tlias84.Service.LoginService;
import com.itheima.tlias84.Util.JwtUtil;
import com.itheima.tlias84.Util.ResultUtil;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.mapper.EmpMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {
   @Resource
   EmpMapper empMapper;

    @Override
    public ResultUtil login(LoginParam param) {
        Emp emp = empMapper.selectEmpByUsername(param.getUsername());
        if(Objects.isNull(emp)||!emp.getPassword().equals(param.getPassword())){
            return ResultUtil.fail("用户名或密码错误");
        }
        String token = JwtUtil.generateToken(param.getUsername(), emp.getPassword(), 100000);

        JSONObject result = new JSONObject();
        result.put("id", emp.getId());
        result.put("username", emp.getUsername());
        result.put("name", emp.getName());
        result.put("token", token);
        return ResultUtil.success(result);


    }
}
